package com.twu.biblioteca.util;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Item;
import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.Movie;

import java.util.Iterator;
import java.util.List;

public class ListMenu implements IMenuEntry {

    public final static String SEPERATOR = "\t|\t" ;
    public final static String NEXT_LINE = "\n" ;

    @Override
    public String handle(String args) {
        String result = new String();
        System.out.println(args);
        List<Item> libraryEntryList = LibraryService.getInstance().getLibrary().getAvailableList(args);
        Iterator<Item> iterator = libraryEntryList.iterator();
        if( args.equalsIgnoreCase(Library.getBookType())) {
            result += formatBookDetails();
            while(iterator.hasNext()){
                Book book = (Book) iterator.next();
                result += formatBookDetails(book);
            }
        } else if( args.equalsIgnoreCase(Library.getMovieType())){
            result += formatMovieDetails();
            while(iterator.hasNext()){
                Movie movie = (Movie) iterator.next();
                result += formatMovieDetails(movie);
            }
        }
        return result;
    }



    private String formatBookDetails(Book book) {
        return book.getBookName() + SEPERATOR + book.getBookCode() + SEPERATOR + book.getBookDescription() + NEXT_LINE;
    }

    private String formatBookDetails() {
        return "BOOK_NAME" + SEPERATOR + "BOOK_CODE" + SEPERATOR + "BOOK_DESCRIPTION" + NEXT_LINE;
    }

    private String formatMovieDetails(Movie movie) {
        return movie.getMovieName() + SEPERATOR + movie.getYear() + SEPERATOR + movie.getDirector() + SEPERATOR + movie.getRating() + NEXT_LINE;
    }

    private String formatMovieDetails() {
        return "MOVIE_NAME" + SEPERATOR + "MOVIE_YEAR" + SEPERATOR + "MOVIE_DIRECTOR" + SEPERATOR + "MOVIE_RATING" + NEXT_LINE;
    }

}
